<template>
  <div class="app-container">
    <el-row :gutter="20">
      <!--      筛选-->
      <!--      分栏24占满 -->
      <el-col :span="24" :xs="24">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
          <el-form-item label="ID" prop="id">
            <el-input
              v-model="queryParams.id"
              placeholder="请输入ID"
              clearable
              style="width: 240px"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="标题" prop="title">
            <el-input
              v-model="queryParams.title"
              placeholder="请输入标题"
              clearable
              style="width: 240px"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="创建者" prop="userId">
            <el-select
              style="width: 240px"
              v-model="queryParams.userId"
              filterable
              remote
              reserve-keyword
              placeholder="请输入创建者昵称"
              :remote-method="remoteGetUserList"
              :loading="userLoading">
              <el-option
                v-for="item in userList"
                :key="item.userId"
                :label="item.nickName"
                :value="item.userId">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="状态" prop="status">
            <el-select
              v-model="queryParams.status"
              placeholder="选择状态"
              clearable
            >
              <el-option
                v-for="dict in enums.sys_normal_disable"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="进度" prop="progress">
            <el-select
              v-model="queryParams.progress"
              placeholder="选择进度"
              clearable
            >
              <el-option
                v-for="dict in enums.COMPETITION_STATUS"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="类型" prop="type">
            <el-select
              v-model="queryParams.type"
              placeholder="选择类型"
              clearable
            >
              <el-option
                v-for="dict in enums.COMPETITION_AUTH"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="赛制" prop="type">
            <el-select
              v-model="queryParams.type"
              placeholder="选择赛制"
              clearable
            >
              <el-option
                v-for="dict in enums.COMPETITION_TYPE"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="创建时间">
            <el-date-picker
              v-model="dateRange"
              value-format="yyyy-MM-dd HH:mm:ss"
              type="datetimerange"
              range-separator="-"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
            ></el-date-picker>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button
              type="primary"
              plain
              icon="el-icon-plus"
              size="mini"
              @click="handleAdd"
            >新增</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="danger"
              plain
              icon="el-icon-delete"
              size="mini"
              :disabled="multiple"
              @click="handleDelete"
            >删除</el-button>
          </el-col>
           <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" :columns="columns"></right-toolbar>
        </el-row>

        <el-table ref="multipleTable"  v-loading="loading" :data="data"  @selection-change="handleSelectionChange" >
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column v-for="(item,index) in columns"
                           v-if="item.visible"
                           :key="index"
                           :label="item.label"
                           :prop="item.prop"
                           :width="item.width"
                           align="center">
            <template slot-scope="scope">
              <span v-if="item.prop =='createTime'"  >{{ parseTime(scope.row[item.prop]) }}</span>
              <el-switch
                v-else-if="item.prop == 'status'"
                v-model="scope.row[item.prop]"
                :active-value="0"
                :inactive-value="1"
                @change="handleStatusChange(scope.row)"
              ></el-switch>
              <span v-else-if="item.prop == 'auth'">
                {{getAuth(scope.row[item.prop])}}
              </span>
              <span v-else-if="item.prop == 'author'">
                {{scope.row['author'] && scope.row['author'].nickName}}
              </span>
              <span v-else-if="item.prop == 'startTime'">
               {{ parseTime(scope.row[item.prop]) }}
              </span>
              <span v-else-if="item.prop == 'endTime'">
               {{ parseTime(scope.row[item.prop]) }}
              </span>
              <span v-else-if="item.prop == 'duration'">
               {{ getSecondsMatch(scope.row[item.prop]) }}
              </span>
              <span v-else-if="item.prop == 'type'">
               {{ enums.getLabelByValue(enums.COMPETITION_TYPE,(scope.row[item.prop])) }}
              </span>
              <span v-else-if="item.prop == 'auth'">
                {{ enums.getLabelByValue(enums.COMPETITION_AUTH,(scope.row[item.prop])) }}
              </span>
              <span v-else >{{ scope.row[item.prop] }}</span>
            </template>
          </el-table-column>
          <el-table-column
            label="操作"
            align="center"
            width="160"
            class-name="small-padding fixed-width"
          >
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="handleUpdate(scope.row)"
              >修改</el-button>
              <el-button
                size="mini"
                type="text"
                icon="el-icon-delete"
                @click="handleDelete(scope.row)"
              >删除</el-button>
              <el-dropdown size="mini" @command="(command) => handleCommand(command, scope.row)" >
                <el-button size="mini" type="text" icon="el-icon-d-arrow-right">更多</el-button>
                <el-dropdown-menu slot="dropdown">
                  <el-dropdown-item command="handleRegisterUser" icon="el-icon-user"
                  >报名列表</el-dropdown-item>
                  <el-dropdown-item command="handleProblemList" icon="el-icon-user"
                  >题目列表</el-dropdown-item>
                </el-dropdown-menu>
              </el-dropdown>
            </template>

          </el-table-column>
        </el-table>

        <pagination
          v-show="total>0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </el-col>
    </el-row>


  </div>
</template>

<script>
import { secondsMatch } from '@/utils/ruoyi'
import * as api from '@/api/contest'
import * as tagApi from '@/api/tag'
import * as problemTypeApi from '@/api/problemType'
import { listUser } from '@/api/system/user'
export default {
  name: "Contest",
  data() {
    return {
      userLoading:false,
      tagLoading:false,
      problemTypeLoading:false,
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 表格数据
      data: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      multiple:true,
      // 日期范围
      dateRange: [],
      // 表单参数
      form: {},
      //选中列表
      ids:[],
      userList:[],
      //题库id
      problemTypeId:"",
      // 查询参数
      queryParams: {
        pageNum: 1,
        userId:'',
        pageSize: 10,
        id:'',
        progress:undefined,
        status:undefined,
        type:undefined,
        auth:undefined,
      },
      tagList:[],
      problemTypeList:[],
      columns: [
        { label: `ID`, visible: true, prop:"id"},
        { label: `标题`, visible: true, prop:"title"},
        { label: `开始时间`, visible: true, prop:"startTime"},
        { label: `结束时间`, visible: true, prop:"endTime"},
        { label: `时长`, visible: true, prop:"duration"},
        { label: `创建人`, visible: true, prop:"author"},
        { label: `赛制`, visible: true, prop:"type"},
        { label: `类型`, visible: true, prop:"auth"},
        { label: `题目数量`, visible: true, prop:"problemNum"},
        { label: `报名人数`, visible: true, prop:"registersNum"},
        { label: `状态`, visible: true, prop:"status"},
        { label: `描述`, visible: false, prop:"description"},
        { label: `创建时间`, visible: false, prop:"createTime" },
        { label: `修改时间`, visible: false, prop:"updateTime" },
    ],
      // 表单校验
      rules: {
        name: [
          { required: true, message: "分类名称不能为空", trigger: "blur" },
          { min: 1, max: 7, message: '分类名称长度必须介于 1 和 7 之间', trigger: 'blur' }
        ]
      },


    };
  },
  watch: {

  },
  computed:{
  },
  created() {
    this.getList();
  },
  methods: {
    getSecondsMatch(date){
      return secondsMatch(date)
    },

    getAuth(auth){
      return this.enums.authList.filter(item => item.value == auth)[0].label
    },
    /** 查询列表 */
    getList() {
      this.loading = true;
      api.list(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
          this.data = response.data.rows;
          this.total = response.data.total;
          this.loading = false;
        }
      );
    },
    /** 状态修改 */
    handleStatusChange(row) {
      let text = row.status === 1 ? "停用" : "启用";
      this.$modal.confirm('确认要'+ text + '？').then(function() {
        return api.changeStatus(row.id, row.status);
      }).then(() => {
        this.$modal.msgSuccess(text + "成功");
      }).catch(function() {
        row.status = row.status === 0 ? 1 : 0;
      });
    },
    /**  表单重置 */
    reset() {
      this.form = {
        id: undefined,
        name: undefined,
        sort: 0,
        status:0,
        remark:undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 多选框选中数据 */
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id);
      this.multiple = !selection.length;
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      let Ids = this.ids;
      if(row && row.id)
        Ids = [row.id]
      this.$modal.confirm('是否确认删除').then(function() {
        return api.del(Ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.$router.push({
        path:`/addContest`,
        query:{
          id:row.id
        }
      });
    },
    /** 新增按钮操作 */
    handleAdd(){
      this.$router.push({
        path:`/addContest`,
      });
    },
    /**参照 **/
    getReferenceInfo(){
      return this.data.filter(item => this.ids.includes(item.id));
    },
    //远程查询用户
    remoteGetUserList(query){
      this.userLoading = true;
      listUser({ nickName: query }).then(res => {
        this.userList = res.rows;
        this.userLoading = false;
      })
    },
    // 更多操作触发
    handleCommand(command, row) {

      console.log(command)
      switch (command) {
        case "handleRegisterUser":
          this.$router.push({
            path:`/registerUsers`,
            query:{ id : row.id }
          });
          break;
        case "handleProblemList":
          this.$router.push({
            path:`/problemList`,
            query:{
              id:row.id
            }
          });
          break;
      }
    },


  }
};
</script>
<style>

</style>
